import json
import os
import random

from myapp import models
from myapp.utils import getPublicData, wordCloudPicture
from myapp.utils.wordCloudPicture import createWordCloudImage2


def getPageData():
    # jobs = models.JobInfo.objects.all()
    # data = [j.address for j in jobs]
    # citys = list(set(data))

    return getPublicData.hotCity


def getSalaryData(city):
    jobs = models.JobInfo.objects.filter(address=city).all()

    # ['0-10k', '10-20k', '20-30k', '30-40k', '40k以上']
    lineData = [0 for i in range(5)]
    for j in jobs:
        if j.pratice > 0:
            continue
        s = json.loads(j.salary)[1] / 1000
        if s < 10:
            lineData[0] += 1
        elif s < 20:
            lineData[1] += 1
        elif s < 30:
            lineData[2] += 1
        elif s < 40:
            lineData[3] += 1
        else:
            lineData[4] += 1

    return getPublicData.salaryList, lineData


def getPeopleData(city):
    jobs = models.JobInfo.objects.filter(address=city).all()

    # ['20人以下', '100人以下', '500人以下', '1000人以下', '1万人以下', '1万人以上']
    data = [0 for i in range(6)]
    for j in jobs:
        p = json.loads(j.companyPeople)[1]
        if p <= 20:
            data[0] += 1
        elif p <= 100:
            data[1] += 1
        elif p <= 500:
            data[2] += 1
        elif p <= 1000:
            data[3] += 1
        elif p < 10000:
            data[4] += 1
        else:
            data[5] += 1

    print(data)
    result = []
    for index, item in enumerate(data):
        if item == 0:
            continue

        result.append({
            'name': getPublicData.peopleList[index],
            'value': item
        })

    # 或者
    # for k, v in zip(getPublicData.peopleList, data):
    #     if v == 0:
    #         continue
    #     result.append({
    #         'name': k,
    #         'value': v
    #     })

    return result


def getEducationalData(city):
    jobs = models.JobInfo.objects.filter(address=city).all()

    data = {}
    for j in jobs:
        if data.get(j.educational, -1) == -1:
            data[j.educational] = 1
        else:
            data[j.educational] += 1

    result = []
    for k, v in data.items():
        if v == 0:
            continue
        result.append({
            'name': k,
            'value': v
        })

    return result


def getDistData(city):
    jobs = models.JobInfo.objects.filter(address=city).all()

    data = {}
    for j in jobs:
        if j.dist == '':
            continue
        if data.get(j.dist, -1) == -1:
            data[j.dist] = 1
        else:
            data[j.dist] += 1

    result = []
    for k, v in data.items():
        result.append({
            'name': k,
            'value': v
        })

    return result


def getCompanyTagsData(city):
    tagText = ''
    result = False
    image = ''

    jobs = models.JobInfo.objects.filter(address=city).all()
    for j in jobs:
        if j.companyTags != '无':
            tags = json.loads(j.companyTags)
            for t in tags:
                tagText += t

    if tagText:
        path = os.getcwd()
        stopWordFile = path + '/myapp/utils/stopwords.txt'
        makeImage = path + '/static/clouds/source/gift.jpg'
        randomNum = random.randint(1, 1000000)
        image = '{}.png'.format(randomNum)
        saveImage = path + '/static/clouds/' + image
        result = createWordCloudImage2(tagText, makeImage, saveImage, stopWordFile)

    return result, image